package com.xiaocikuai.smallchunks.service.Transaction;

import com.alibaba.fastjson.JSONObject;
import com.xiaocikuai.smallchunks.config.ZTOExpressConfig;
import com.xiaocikuai.smallchunks.entity.Transaction.Address;
import com.xiaocikuai.smallchunks.mapper.Transaction.TransactionMapper;
import com.zto.zop.EncryptionType;
import com.zto.zop.ZopClient;
import com.zto.zop.ZopPublicRequest;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.io.IOException;


@Service
public class ZTOService {

    @Resource
    ZTOExpressConfig ztoExpressConfig;

    @Resource
    private TransactionMapper transactionMapper;

    public String ZTOAddOrder(int aid, String orderId, String tName, String version) throws IOException {
        ZopClient client = new ZopClient(ztoExpressConfig.appKey, ztoExpressConfig.appSecret);
        ZopPublicRequest request = new ZopPublicRequest();

        JSONObject accountInfo = new JSONObject();
        accountInfo.put("accountId", "D36_18629595153");
        accountInfo.put("accountPassword", "4B1QNL9Q");
        accountInfo.put("type", 1);
//        accountInfo.put("customerId", "GPG1576724269");

        JSONObject senderInfo = new JSONObject();
//        senderInfo.put("senderId", );
        senderInfo.put("senderName", "张舟");
//        senderInfo.put("senderPhone", );
        senderInfo.put("senderMobile", "19992787088");
        senderInfo.put("senderProvince", "陕西省");
        senderInfo.put("senderCity", "西安市");
        senderInfo.put("senderDistrict", "长安区");
        senderInfo.put("senderAddress", "大兆乡雁引公路东伍村村民委员会中通快递");

        Address address = transactionMapper.getAddressInfo(aid);
        JSONObject receiverInfo = new JSONObject();
        receiverInfo.put("receiverName", address.getConsignee());
//        receiverInfo.put("receiverPhone",);
        receiverInfo.put("receiverMobile", address.getTelephone());
        receiverInfo.put("receiverProvince", address.getProv());
        receiverInfo.put("receiverCity", address.getCity());
        receiverInfo.put("receiverDistrict", address.getArea());
        receiverInfo.put("receiverAddress", address.getDelivery());

        JSONObject orderItem = new JSONObject();
        orderItem.put("name", tName + "【" + version + "】");

        JSONObject requestBody = new JSONObject();
        requestBody.put("partnerType", "2");//合作模式 ，1：集团客户；2：非集团客户
        requestBody.put("orderType", "2");//1：全网件 2：预约件（返回运单号） 3：预约件（不返回运单号） 4：星联全网件
        requestBody.put("partnerOrderCode", orderId);//合作商订单号
        requestBody.put("accountInfo", accountInfo);
        requestBody.put("senderInfo", senderInfo);
        requestBody.put("receiveInfo", receiverInfo);
        requestBody.put("siteCode", "029001");
        requestBody.put("siteName", "西安雁南路");
        requestBody.put("remark", "【小词块】微语境背单词");
        requestBody.put("orderItems", new JSONObject[]{orderItem});
//        requestBody.put("cabinet", cabinet);

        request.setBody(requestBody.toString());
        request.setUrl("https://japi.zto.com/zto.open.createOrder");
        request.setBase64(true);
        request.setEncryptionType(EncryptionType.MD5);
        request.setTimestamp(null);

        System.out.println(client.execute(request));
        return client.execute(request);

    }


    public JSONObject ZTOGetInfo (String billCode) throws IOException {
        ZopClient client = new ZopClient(ztoExpressConfig.appKey, ztoExpressConfig.appSecret);
        ZopPublicRequest request = new ZopPublicRequest();

        JSONObject requestBody = new JSONObject();
        requestBody.put("billCode", billCode);

        request.setBody(requestBody.toString());
        request.setUrl("https://japi.zto.com/zto.merchant.waybill.track.query");
        request.setBase64(true);
        request.setEncryptionType(EncryptionType.MD5);
        request.setTimestamp(null);
        System.out.println(client.execute(request));
        return parseBillCode(client.execute(request));

    }


    public static JSONObject parseBillCode(String jsonStr) {
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        return jsonObject;
    }


}
